package cn.zyn.eduservice.controller;


import cn.zyn.commonutils.R;
import cn.zyn.eduservice.entity.EduCourse;
import cn.zyn.eduservice.entity.vo.CourseInfoVo;
import cn.zyn.eduservice.entity.vo.CoursePublishVo;
import cn.zyn.eduservice.entity.vo.CourseQuery;
import cn.zyn.eduservice.entity.vo.TeacherQuery;
import cn.zyn.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 */
@Api(description = "课程管理")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    //课程列表 TODO 完善条件分页查询课程列表
    //条件分页查询课程列表
    @ApiOperation(value = "条件分页课程列表")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R getpageCourseCondition(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable Long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询条件对象", required = false)
            @RequestBody(required = false) CourseQuery courseQuery){
        //创建Page对象
        //两个参数current代表当前页   limit代表每页记录数
        Page<EduCourse> coursePage = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //多条件组合查询  mybatis 动态sql if
        //判断条件值是否为空
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        //name是表中的字段名
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(begin)) {
            //ge大于等于
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            //le小于等于
            wrapper.le("gmt_create", end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现分页
        //调用方法时，底层封装，把分页所有数据封装到pageTeacher对象里面
        courseService.page(coursePage, wrapper);
        //每页数据
        List<EduCourse> records = coursePage.getRecords();
        //获取总记录数
        long total = coursePage.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }


    @ApiOperation(value = "查询课程列表")
    //查询课程列表
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }


    @ApiOperation(value = "添加课程基本信息")
    //添加课程基本信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        //返回添加之后的课程id 为了后面添加课程的大纲
        String id =courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }


    @ApiOperation(value = "根据课程id进行查询课程基本信息")
    //2.根据课程id进行查询课程基本信息
    @GetMapping("getCourseInfo/{courseid}")
    public R getCourseInfo(@PathVariable String courseid){
        CourseInfoVo courseInfoVo =courseService.getCourseInfo(courseid);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }


    @ApiOperation(value = "修改课程信息")
    //3.修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }


    @ApiOperation(value = "根据课程Id查询课程确认信息")
    //4.根据课程Id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{cid}")
    public R getPublishCourseInfo(@PathVariable String cid){
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(cid);
        return R.ok().data("publishCourse",coursePublishVo);
    }


    @ApiOperation(value = "课程最终发布 修改课程状态")
    //5.课程最终发布 修改课程状态
    @PostMapping("publishCourse/{cid}")
    public R publishCourse(@PathVariable String cid){
        EduCourse course = new EduCourse();
        course.setId(cid);
        course.setStatus("Normal");
        courseService.updateById(course);
        return R.ok();
    }


    //6.删除课程
    @ApiOperation(value = "删除课程")
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return  R.ok();
    }


}

